package cn.imnaer.service.rpc.schedule.impl;

import java.util.ArrayList;
import java.util.List;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import cn.imnaer.common.factory.TimeHelper;
import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.persistence.model.Schedule;
import cn.imnaer.service.persistence.model.ScheduleItem;
import cn.imnaer.service.persistence.model.consts.SCHEDULE;
import cn.imnaer.service.rest.resquest.model.AppendScheduleParams;
import cn.imnaer.service.rest.resquest.model.ScheduleCreateParams;
import cn.imnaer.service.rest.view.model.SimpleSchedule;
import cn.imnaer.service.rpc.scenic.process.ScenicRepository;
import cn.imnaer.service.rpc.schedule.ScheduleComponentRpcApi;
import cn.imnaer.service.rpc.schedule.process.ScheduleDbHelper;

public class ScheduleComponentRpcApiImpl implements ScheduleComponentRpcApi {
    @Override
    public Schedule create(ScheduleCreateParams scheduleCreateParams) {

        Schedule schedule = scheduleCreateParams.transformSchedule();
        if (scheduleCreateParams.getId() != null && !scheduleCreateParams.getId().isEmpty()) {
            List<ScheduleItem> scheduleItems = new ArrayList<ScheduleItem>();
            ScheduleItem e = new ScheduleItem();
            switch (scheduleCreateParams.getType()) {
                case ScheduleItem.TYPE_SCENIC:
//                    Scenic scenic = new ScenicRepository(scheduleCreateParams.getId()).getScenicInfo();
//                    e.setImage(scenic.getImage());
//                    e.setScheduleItemId(scheduleCreateParams.getId());
//                    e.setType(ScheduleItem.TYPE_SCENIC);
//                    e.setScenicName(scenic.getTitle());
                    break;
                default:
                    break;
            }
            scheduleItems.add(e);
            schedule.setScheduleItems(scheduleItems);
            schedule.setImage(e.getImage());
        }
        new ScheduleDbHelper().insert(schedule,true);
        return schedule;
    }

    @Override
    public List<SimpleSchedule> getScheduleList(String userId) {
        List<SimpleSchedule> record = new ArrayList<SimpleSchedule>();

//        BSONObject matcher = new BasicBSONObject();
//        matcher.put(SCHEDULE.userId, userId);
//        try {
//            BSONObject selector = BasicBSONObject.typeToBson(new SimpleSchedule());
//         Object list = new ScheduleDbHelper().findAll(matcher, SimpleSchedule.class,null);
//            for (BSONObject bsonObject : list) {
//                record.add(bsonObject.as(SimpleSchedule.class));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return record;
    }


    @Override
    public void update(Schedule schedule) {

        schedule.setCreateTime(TimeHelper.getCurrentTime());
        List<ScheduleItem> items = schedule.getScheduleItems();
        int itemSize = items.size();
        if (itemSize > 0) {
            //第一个item的image明显可能是空的
            for (int i = 0; i < itemSize; i++) {
                String image = items.get(i).getImage();
                if (image != null && !image.isEmpty()) {
                    schedule.setImage(image);
                    break;
                }
            }
        }
        BSONObject matcher = new BasicBSONObject();
        matcher.put(SCHEDULE.scheduleId, schedule.getScheduleId());
//        new ScheduleDbHelper().updateObject(matcher, schedule);
        schedule = null;


    }

    @Override
    public Schedule getScheduleDetails(String scheduleId) {
        Schedule schedule = null;

//        BasicBSONObject query = new BasicBSONObject();
//        query.put(SCHEDULE.scheduleId, scheduleId);
////        BSONObject result = new ScheduleDbHelper().queryObject(query);
//        try {
//            if (result != null)
//                schedule = result.as(Schedule.class);
//            return schedule;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return schedule;
    }

    @Override
    public void append(AppendScheduleParams params) {
//        ScheduleItem item = params.transformScheduleItem();
//
//        BasicBSONObject query = new BasicBSONObject();
//        query.put(SCHEDULE.scheduleId, params.getScheduleId());
//        BSONObject bsonObject = new ScheduleDbHelper().queryObject(query);
//        try {
//            Schedule schedule = bsonObject.as(Schedule.class);
//            if (schedule != null) {
//                schedule.getScheduleItems().add(item);
//                new ScheduleDbHelper().updateObject(query, schedule);
//            }
//        } catch (Exception e) {
//
//            e.printStackTrace();
//        }


    }

    @Override
    public void deleteSchedule(List<String> list) {


        System.out.println(list.size());
        new ScheduleDbHelper().deleteObejcts(list);


    }

}
